package com.shop2cn.iapi.coupon.testcase.old;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.coupon.parameter.UseCouponBean;
import com.shop2cn.iapi.coupon.parameter.updateReceivedAndUsedCountBean;
import com.shop2cn.iapi.coupon.service.UserReceiveCouponCall;
import com.shop2cn.iapi.coupon.service.updateReceivedAndUsedCountCall;
import com.shop2cn.iapi.couponmanage.parameter.CreateBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.parameter.executeTaskBean;
import com.shop2cn.iapi.couponmanage.service.CreateCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.shop2cn.iapi.couponmanage.service.executeTaskCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnCallService;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponupdatesetting;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponsendsubtaskWapper;
import com.ymttest.database.sqlwapper.couponupdatesettingWapper;
import com.ymttest.utils.TestDataManager;
/**
*更新优惠券批次已领取数量和已使用数量
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_updateReceivedAndUsedCount{
	private static updateReceivedAndUsedCountBean updatereceivedandusedcountBean;
	private static updateReceivedAndUsedCountCall updatereceivedandusedcountCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新优惠券批次已领取数量和已使用数量");
	}

	@Before
	public void caseUp() {
		updatereceivedandusedcountBean=new updateReceivedAndUsedCountBean();
		updatereceivedandusedcountCall=new updateReceivedAndUsedCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatereceivedandusedcountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateReceivedAndUsedCount_001() {
		Logger.start(true,"更新优惠券批次已领取数量和已使用数量-平台-更新已领取数量");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
			
			couponbatch couponbatch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			UserReceiveCouponCall userReceiveCouponCall = CouponShop2cnMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);

			//更新couponupdatesetting为当前时间
			couponupdatesetting couponupdatesetting=new couponupdatesettingWapper().selectTop1();
			couponupdatesetting.setLastUpdateTime(new Date());
			new couponupdatesettingWapper().updateByPrimaryKey(couponupdatesetting);
			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
			updatereceivedandusedcountCall.callService();	
			
			Thread.sleep(3000);
			couponbatch couponbatch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			//Logger.verifyEquals(1, couponbatch.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, couponbatch2.getReceivedCount()-couponbatch1.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateReceivedAndUsedCount_002() {
		Logger.start(true,"更新优惠券批次已领取数量和已使用数量-平台-更新已领取数量和已使用数量");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
			
			couponbatch couponbatch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			UserReceiveCouponCall userReceiveCouponCall = CouponShop2cnMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			coupon coupon=new couponWapper().selectByBatchId(couponbatch1.getBatchId()).get(0);
			//使用券
			CouponShop2cnCallService couponService=new CouponShop2cnCallService();
			UseCouponBean useCouponBean=new UseCouponBean();
			List<String> couponCodes=new ArrayList();
			couponCodes.add(coupon.getCouponCode());
			useCouponBean.setUserId(userId);
			useCouponBean.setCouponCodes(couponCodes);
			useCouponBean.setMainOrderId(orderId);
			couponService.useCoupon(useCouponBean);
			//更新couponupdatesetting为当前时间
			couponupdatesetting couponupdatesetting=new couponupdatesettingWapper().selectTop1();
			couponupdatesetting.setLastUpdateTime(new Date());
			new couponupdatesettingWapper().updateByPrimaryKey(couponupdatesetting);
			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
			updatereceivedandusedcountCall.callService();	
			
			Thread.sleep(3000);
			couponbatch couponbatch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			Logger.verifyEquals(1, couponbatch2.getUsedCount()-couponbatch1.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, couponbatch2.getReceivedCount()-couponbatch1.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateReceivedAndUsedCount_003() {
		Logger.start(true,"更新优惠券批次已领取数量和已使用数量-商家-已领取数量-不用此接口更新");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean);
		
			couponbatch couponbatch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			UserReceiveCouponCall userReceiveCouponCall = CouponShop2cnMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);

			Thread.sleep(3000);
			couponbatch couponbatch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			//Logger.verifyEquals(1, couponbatch.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, couponbatch2.getReceivedCount()-couponbatch1.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateReceivedAndUsedCount_008() {
		Logger.start(true,"更新优惠券批次已领取数量和已使用数量-商家-已使用数量-不用此接口更新");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			couponbatch couponbatch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			UserReceiveCouponCall userReceiveCouponCall = CouponShop2cnMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			coupon coupon=new couponWapper().selectByBatchId(couponbatch1.getBatchId()).get(0);
			//使用券
			CouponShop2cnCallService couponService=new CouponShop2cnCallService();
			UseCouponBean useCouponBean=new UseCouponBean();
			List<String> couponCodes=new ArrayList();
			couponCodes.add(coupon.getCouponCode());
			useCouponBean.setUserId(userId);
			useCouponBean.setCouponCodes(couponCodes);
			useCouponBean.setMainOrderId(orderId);
			couponService.useCoupon(useCouponBean);
//			//更新couponupdatesetting为当前时间
//			couponupdatesetting couponupdatesetting=new couponupdatesettingWapper().selectTop1();
//			couponupdatesetting.setLastUpdateTime(new Date());
//			new couponupdatesettingWapper().updateByPrimaryKey(couponupdatesetting);
//			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
//			updatereceivedandusedcountCall.callService();	
			
			//平台券异步JOB回调更新批次已领取和使用数量，放到Job,目前为了测试方便，直接调用
			new CouponShop2cnMCallService().updatereceivedandusedcount();
			
			couponbatch couponbatch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			Logger.verifyEquals(1, couponbatch2.getUsedCount()-couponbatch1.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, couponbatch2.getReceivedCount()-couponbatch1.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateReceivedAndUsedCount_005() {
		Logger.start(true,"更新优惠券批次已领取数量和已使用数量-兑换随机券-已领取数量-结果：不更新，应使用兑换优惠券");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
		
			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = couponMCallService.setDefaultRandomCreateBean(batchCodes);
			CreateCall createCall = couponMCallService.create(createBean);
			couponbatch couponbatch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			// 这里直接调用job接口生成兑换码
			List<Map>  tasks=new couponsendsubtaskWapper().selectTaskByTaskTypelimit(3,1);
			executeTaskCall call=new executeTaskCall();
			executeTaskBean bean=new executeTaskBean();
			
			for(int i=0;i<tasks.size();i++){
				bean.setSubTaskId(Integer.parseInt(tasks.get(i).get("SubTaskId").toString()));
				call.setData(bean);
				call.callService();
			}
			
			
			couponupdatesetting couponupdatesetting=new couponupdatesettingWapper().selectTop1();
			couponupdatesetting.setLastUpdateTime(new Date());
			new couponupdatesettingWapper().updateByPrimaryKey(couponupdatesetting);
			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
			updatereceivedandusedcountCall.callService();	
			
//			UserReceiveCouponCall userReceiveCouponCall = CouponShop2cnMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);

			
			couponbatch couponbatch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			//Logger.verifyEquals(1, couponbatch.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(0, couponbatch2.getReceivedCount()-couponbatch1.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateReceivedAndUsedCount_006() {
		Logger.start(true,"更新优惠券批次已领取数量和已使用数量-兑换券-已使用数量");
		try {
			
//			int userId = TestDataManager.getRandomIntNum(8);
//			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
//			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();
//
//			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
//		
//			List<String> batchCodes = new ArrayList<>();
//			batchCodes.add(createCouponCall.getBatchCode());
//
//			CreateBean createBean = couponMCallService.setDefaultRandomCreateBean(batchCodes);
//			CreateCall createCall = couponMCallService.create(createBean);
//			couponbatch couponbatch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
//			
//			// 这里直接调用job接口生成兑换码
//			List<Map>  tasks=new couponsendsubtaskWapper().selectTaskByTaskTypelimit(3,1);
//			executeTaskCall call=new executeTaskCall();
//			executeTaskBean bean=new executeTaskBean();
//			
//			for(int i=0;i<tasks.size();i++){
//				bean.setSubTaskId(Integer.parseInt(tasks.get(i).get("SubTaskId").toString()));
//				call.setData(bean);
//				call.callService();
//			}
//			
//			//更新couponupdatesetting为当前时间
//			couponupdatesetting couponupdatesetting=new couponupdatesettingWapper().selectTop1();
//			couponupdatesetting.setLastUpdateTime(new Date());
//			new couponupdatesettingWapper().updateByPrimaryKey(couponupdatesetting);
//			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
//			updatereceivedandusedcountCall.callService();	
//			
////			UserReceiveCouponCall userReceiveCouponCall = CouponShop2cnMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
//
//			Thread.sleep(3000);
//			couponbatch couponbatch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
//			//Logger.verifyEquals(1, couponbatch.getUsedCount(), "验证数据库couponbatch中UsedCount");
//			Logger.verifyEquals(1, couponbatch2.getReceivedCount()-couponbatch1.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateReceivedAndUsedCount_004() {
		Logger.start(false,"更新优惠券批次已领取数量和已使用数量-无更新数据");
		try {
			
			couponupdatesetting couponupdatesetting=new couponupdatesettingWapper().selectTop1();
			couponupdatesetting.setLastUpdateTime(new Date());
			new couponupdatesettingWapper().updateByPrimaryKey(couponupdatesetting);
			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
			updatereceivedandusedcountCall.callService();	
			
			//第一次更新完数据
			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
			updatereceivedandusedcountCall.callService();	
			//第二次无更新数据
			updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
			updatereceivedandusedcountCall.callService();
			
			Logger.verifyEquals("ok", updatereceivedandusedcountCall.getReturnData(), "验证成功返回ok");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}